package com.d4p.util
{
	
	/**-----------------------------------------------------
	 * Use following BitmapPatternBuilder class 
	 * 
	 * DO NOT CHANGE any codes below this comment.
	 *
	 * -----------------------------------------------------
	 */
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	
	public class BitmapPatternBuilder{
		/**
		 * 
		 * creates BitmapData filled with dot pattern.
		 * First parameter is 2d array that contains color index for each pixels;
		 * Second parameter contains color reference table.
		 *
		 * 
		 * 
		 * @parameter pattern:Array 2d array that contains color index for each pixel.
		 * @parameter colors:Array 1d array that contains color table.
		 * @returns BitmapData
		 * 
		 */
		public static function build(pattern:Array, colors:Array):BitmapData{
			var bitmapW:int = pattern[0].length;
			var bitmapH:int = pattern.length;
			var bmd:BitmapData = new BitmapData(bitmapW,bitmapH,true,0x000000);
			for(var yy:int=0; yy<bitmapH; yy++){
				for(var xx:int=0; xx<bitmapW; xx++){
					var color:int = colors[pattern[yy][xx]];
					bmd.setPixel32(xx, yy, color);
				}
			}
			return bmd;
		}
		
		/**
		 * short cut function for Graphics.beginBitmapFill with pattern.
		 */
		public static function beginBitmapFill(pattern:Array, colors:Array, graphics:Graphics):void{
			var bmd:BitmapData = build(pattern, colors);
			graphics.beginBitmapFill(bmd);
			bmd.dispose();        
		}
	}
}